<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="adminId">
      <el-input v-model="dataForm.adminId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="goodsCount">
      <el-input v-model="dataForm.goodsCount" placeholder="商品数量"></el-input>
    </el-form-item>
    <el-form-item label="商品单价" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="商品单价"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="payDate">
      <el-input v-model="dataForm.payDate" placeholder="支付时间"></el-input>
    </el-form-item>
    <el-form-item label="收货地址ID" prop="deliveryAddrId">
      <el-input v-model="dataForm.deliveryAddrId" placeholder="收货地址ID"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          adminId: '',
          goodsId: '',
          goodsName: '',
          orderStatus: '',
          goodsCount: '',
          goodsPrice: '',
          createTime: '',
          payDate: '',
          deliveryAddrId: ''
        },
        dataRule: {
          adminId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          goodsCount: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '商品单价不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          payDate: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ],
          deliveryAddrId: [
            { required: true, message: '收货地址ID不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/community/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.adminId = data.order.adminId
                this.dataForm.goodsId = data.order.goodsId
                this.dataForm.goodsName = data.order.goodsName
                this.dataForm.orderStatus = data.order.orderStatus
                this.dataForm.goodsCount = data.order.goodsCount
                this.dataForm.goodsPrice = data.order.goodsPrice
                this.dataForm.createTime = data.order.createTime
                this.dataForm.payDate = data.order.payDate
                this.dataForm.deliveryAddrId = data.order.deliveryAddrId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/community/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'adminId': this.dataForm.adminId,
                'goodsId': this.dataForm.goodsId,
                'goodsName': this.dataForm.goodsName,
                'orderStatus': this.dataForm.orderStatus,
                'goodsCount': this.dataForm.goodsCount,
                'goodsPrice': this.dataForm.goodsPrice,
                'createTime': this.dataForm.createTime,
                'payDate': this.dataForm.payDate,
                'deliveryAddrId': this.dataForm.deliveryAddrId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
